<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Admin_Controller extends MY_Controller {

	var $navigation;
	/**
	 *
	 * Admin form object
	 * @var AdminForm
	 */
	public $form;
	/**
	 *
	 * Admin Grid object
	 * @var AdminGrid
	 */
	public $grid;

	function __construct () {
		parent::__construct();

		define("_CONTROLPANEL","TRUE");

		$this->load->helper('ssl');

		$this->load->model('admin/admin_model');
		if (!$this->admin_model->logged_in()){
			$this->_redirect(admin_url('admin/index/login'));
			die();
		}
		$this->load->library('admin_navigation');
		$this->admin_navigation->parent_link('dashboard','Bảng điều khiển',admin_url('*/dashboard/index'));
		$this->admin_navigation->parent_link('news', $this->__('Tin Tức'));
		$this->admin_navigation->parent_link('general', 'General', admin_url('*/general/form_general'));
		$this->admin_navigation->parent_link('restaurant', 'Quán ăn', admin_url('*/restaurant/manager_restaurant'));
		$this->admin_navigation->parent_link('dish', $this->__('Món ăn'));
		$this->admin_navigation->parent_link('pref', 'Tỉnh thành', admin_url('*/pref/manager_pref'));

		$this->load->helper('string');
		$this->load->helper('directory');
		$this->load->helper('form');

		$this->load->library('headassets');


		$this->load->driver('cache');

		$modules = $this->module_model->get_module_folders();
		//var_dump($modules);

		$this->module_definitions = new stdClass();

		foreach ($modules as $module) {
			MY_Loader::define_module($module . '/');
		}

		$this->_setForm();
		$this->_setGrid();
	}

	private function _setForm(){
		if (is_null($this->form)){
			$this->load->library('AdminForm');
			$this->form = new AdminForm();
		}
		return $this->form;
	}

	private function _setGrid(){
		if (is_null($this->grid)){
			$this->load->library('AdminGrid');
			$this->grid = new AdminGrid();
		}
		return $this->grid;
	}

	function save_and_contine(){

	}
	
	function watermark($filename){
		$this->load->library('image_lib');
        $image_cfg = array();
        $image_cfg['image_library'] = 'GD2';
        $image_cfg['source_image'] = $filename;
        $image_cfg['wm_overlay_path'] = './images/logo/' . 'logo_null.png';
        $image_cfg['wm_type'] = 'overlay';
        $image_cfg['wm_opacity'] = '10';
        $image_cfg['wm_vrt_alignment'] = 'bottom';
        $image_cfg['wm_hor_alignment'] = 'right';
		$image_cfg['wm_hor_alignment'] = 'right';
		$image_cfg['wm_vrt_offset'] = '10';
		$image_cfg['wm_hor_offset'] = '10';
        $image_cfg['create_thumb'] = FALSE;

        $this->image_lib->initialize($image_cfg);
        $this->image_lib->watermark();
        $this->image_lib->clear();

		//echo $this->image_lib->display_errors();
		//die();
    }

    function do_thumb($filename) {
		$this->load->library('image_lib');
        $image_cfg['image_library'] = 'GD2';
        $image_cfg['source_image'] = $filename;
        $image_cfg['create_thumb'] = TRUE;
        $image_cfg['maintain_ratio'] = TRUE;
        $image_cfg['width'] = '280';
        $image_cfg['height'] = '200';
        $this->load->library('image_lib');
        $this->image_lib->initialize($image_cfg);
        $this->image_lib->resize();
        $this->image_lib->clear();
    }
}